from django.core.management.base import BaseCommand
from django.utils import timezone
from Backend.models import UserAccount


class Command(BaseCommand):
    help = "初始化用户数据"

    def handle(self, *args, **options):
        try:
            # 检查用户是否已存在
            if UserAccount.objects.filter(username="ghost").exists():
                self.stdout.write(self.style.WARNING('用户 "ghost" 已存在，跳过创建'))
            else:
                user = UserAccount.objects.create_user(
                    name="ghost", 
                    username="ghost",
                    password="123456",
                    tag_line="无处安放的灵魂",
                    birthday=timezone.make_aware(timezone.datetime(2003, 8, 10)),
                    permission="user",
                    avator=b"",
                    view_nums="0",
                    otherInfos={
                        "zhihu": "https://www.zhihu.com",
                        "bilibili": "https://space.bilibili.com",
                        "github": "https://github.com",
                        "twitter": "#",
                    },
                )

                self.stdout.write(self.style.SUCCESS(f"成功创建用户: {user.username}"))

        except Exception as e:
            self.stdout.write(self.style.ERROR(f"创建用户失败: {e}"))
